﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AppCombine.ascx.cs" Inherits="CRMWeb.UserControls.AppCombine" %>

<div id="dlgAppCombine" class="easyui-window" title="Application Combine" data-options="iconCls:'icon-save',modal:true,closed:true" style="width: 500px; height: 350px; padding: 5px;">

    <div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'center'" style="padding: 10px;">
            <div class="fitem combin_toppart">
                <label>Combine Application-id:</label>
                <input id="txtappid" type="text" class="easyui-validatebox" />
                <a href="javascript:void(0)" class="easyui-linkbutton" id="btnSearch" onclick="SearchCombineApplicants()">Search</a>
            </div>

            <div class="combin_cont">
                <div class="content_sectionone">
                    <label id="lblFirstContact" class="checkbox_title">--- ---</label><br />

                    <table class="checkbox_table" cellpadding="0" cellspacing="0" border="0">
                        <tbody>
                            <tr>
                                <td>
                                    <input type="radio" id="rdoFirstAppli02" name="check1" value="2" /></td>
                                <td>
                                    <label>Applicant 02</label></td>
                            </tr>
                            <tr>
                                <td>
                                    <input type="radio" id="rdoFirstAppli03" name="check1" value="3" /></td>
                                <td>
                                    <label>Applicant 03</label></td>
                            </tr>
                            <tr>
                                <td>
                                    <input type="radio" id="rdoFirstAppli00" name="check1" value="0" /></td>
                                <td>
                                    <label>None</label></td>
                            </tr>

                        </tbody>
                    </table>

                </div>


                <div class="content_sectionone">

                    <label id="lblSecondContact" class="checkbox_title">--- ---</label><br />
                    <table class="checkbox_table" cellpadding="0" cellspacing="0" border="0">
                        <tbody>
                            <tr>
                                <td>
                                    <input type="radio" id="rdoSecondAppli02" name="check2" value="2" /></td>
                                <td>
                                    <label>Applicant 02</label></td>
                            </tr>
                            <tr>
                                <td>
                                    <input type="radio" id="rdoSecondAppli03" name="check2" value="3" /></td>
                                <td>
                                    <label>Applicant 03</label></td>
                            </tr>
                            <tr>
                                <td>
                                    <input type="radio" id="rdoSecondAppli00" name="check2" value="0" /></td>
                                <td>
                                    <label>None</label></td>
                            </tr>
                        </tbody>
                    </table>

                </div>

                <div class="content_sectionone">
                    <label id="lblThirdContact" class="checkbox_title">--- ---</label><br />
                    <table class="checkbox_table" cellpadding="0" cellspacing="0" border="0">
                        <tbody>
                            <tr>
                                <td>
                                    <input type="radio" id="rdoThirdAppli02" name="check3" value="2" /></td>
                                <td>
                                    <label>Application 02</label></td>
                            </tr>
                            <tr>
                                <td>
                                    <input type="radio" id="rdoThirdAppli03" name="check3" value="03" /></td>
                                <td>
                                    <label>Application 03</label></td>
                            </tr>
                            <tr>
                                <td>
                                    <input type="radio" id="rdoThirdAppli00" name="check3" value="0" /></td>
                                <td>
                                    <label>None</label></td>
                            </tr>
                        </tbody>
                    </table>

                </div>

            </div>

            <div data-options="region:'south',border:false"  padding: 5px 0 0;">
                <div style="margin-top: 5px">
                    <span id="spnNotExist" style="font-size: 12px; font-family: Verdana; font-weight: bold; color: red; float: left; display: none">This application has been Deleted </span>
                    <a href="javascript:void(0)" class="easyui-linkbutton" id="lnkbtnSaveApplicationCombine" style="float: right" iconcls="icon-ok" onclick="InsertCombineApp()">Save</a>
                </div>
                <input type="hidden" id="hdnFirstContactId" />
                <input type="hidden" id="hdnSecondContactId" />
                <input type="hidden" id="hdnThirdContactId" />
                <input type="hidden" id="hdnFirstSelect" />
                <input type="hidden" id="hdnSecondSelect" />
                <input type="hidden" id="hdnThirdSelect" />

                <input type="hidden" id="CombineApp1" />
                <input type="hidden" id="CombineApp2" />
                <input type="hidden" id="CombineApp3" />
            </div>
        </div>

    </div>
</div>
<script type="text/javascript">
    var userName = '';

    function getQuerystring(key, defaultValue) {
        if (defaultValue == null) defaultValue = "";
        key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
        var qs = regex.exec(window.location.href);
        if (qs == null)
            return defaultValue;
        else {
            return qs[1];
        }
    }
    function getEachRadioButtonValue() {
        //First:
        var Fval = document.getElementsByName('check1');
        for (var i = 0; i < Fval.length; i++) {
            if (Fval[i].checked) {
                var fvalue = Fval[i].value;
                $("#hdnFirstSelect").val(fvalue);
                break;
            }
        }

        var Sval = document.getElementsByName('check2');
        for (var i = 0; i < Sval.length; i++) {
            if (Sval[i].checked) {
                var svalue = Sval[i].value;
                $("#hdnSecondSelect").val(svalue);
                break;
            }
        }

        var Tval = document.getElementsByName('check3');
        for (var i = 0; i < Tval.length; i++) {
            if (Tval[i].checked) {
                var tvalue = Tval[i].value;
                $("#hdnThirdSelect").val(tvalue);
                break;
            }
        }

    }
    function resetForm() {
        $("#hdnFirstContactId").val('');
        $("#hdnFirstSelect").val('');
        $("#hdnFirstContactId").val('');

        $("#hdnSecondContactId").val('');
        $("#hdnSecondSelect").val('');
        $("#hdnSecondContactId").val('');

        $("#hdnThirdContactId").val('');
        $("#hdnThirdSelect").val('');
        $("#hdnThirdContactId").val('');

        $('#lblFirstContact').html("--- ---");
        $('#lblSecondContact').html("--- ---");
        $('#lblThirdContact').html("--- ---");
        document.getElementById("rdoFirstAppli02").checked = false;
        document.getElementById("rdoFirstAppli03").checked = false;
        document.getElementById("rdoFirstAppli00").checked = false;
        document.getElementById("rdoSecondAppli02").checked = false;
        document.getElementById("rdoSecondAppli03").checked = false;
        document.getElementById("rdoSecondAppli00").checked = false;
        document.getElementById("rdoThirdAppli02").checked = false;
        document.getElementById("rdoThirdAppli03").checked = false;
        document.getElementById("rdoThirdAppli00").checked = false;
    }
    $(document).ready(function () {
        userName = $('#ContentPlaceHolder1_appProfile_hdnUserName').val();
    });
    function SearchCombineApplicants() {
        resetForm();
        var appid = $('#txtappid').val();
        $.ajax({
            data: { app_id: appid },
            url: '/WebServices/SearchCombineApplicants.ashx',
            success: function (data) {
                if (data == "0") {
                    $('#spnNotExist').show();
                    $('#lblFirstContact').html("--- ---");
                    $('#lblSecondContact').html("--- ---");
                    $('#lblThirdContact').html("--- ---");
                }
                else {
                    var arrObj = $.parseJSON(data);
                    $('#spnNotExist').hide();
                    var i = arrObj.length;
                    if (i == 1) {
                        $('#lblFirstContact').html(arrObj[0].FirstName + '  ' + arrObj[0].SurName);
                        $("#hdnFirstContactId").val(arrObj[0].ContactID);
                        $("#CombineApp1").val(arrObj[0].combineApp);
                    }
                    else if (i == 2) {
                        $('#lblFirstContact').html(arrObj[0].FirstName + '  ' + arrObj[0].SurName);
                        $("#hdnFirstContactId").val(arrObj[0].ContactID);
                        $("#CombineApp1").val(arrObj[0].combineApp);

                        $('#lblSecondContact').html(arrObj[1].FirstName + '  ' + arrObj[1].SurName);
                        $("#hdnSecondContactId").val(arrObj[1].ContactID);
                        $("#CombineApp2").val(arrObj[1].combineApp);

                    }
                    else if (i == 3) {

                        $('#lblFirstContact').html(arrObj[0].FirstName + '  ' + arrObj[0].SurName);
                        $("#hdnFirstContactId").val(arrObj[0].ContactID);
                        $("#CombineApp1").val(arrObj[0].combineApp);

                        $('#lblSecondContact').html(arrObj[1].FirstName + '  ' + arrObj[1].SurName);
                        $("#hdnSecondContactId").val(arrObj[1].ContactID);
                        $("#CombineApp2").val(arrObj[1].combineApp);

                        $('#lblThirdContact').html(arrObj[2].FirstName + '  ' + arrObj[2].SurName);
                        $("#hdnThirdContactId").val(arrObj[1].ContactID);
                        $("#CombineApp3").val(arrObj[1].combineApp);
                    }
                }
            }
        });
    }

    function saveHistory(hAction, hDesc) {
       // alert(hAction + hDesc);
        var currentDate = new Date()
        var day = currentDate.getDate()
        var month = currentDate.getMonth() + 1
        var year = currentDate.getFullYear()
        var HistoryDate = month + "/" + day + "/" + year;

        var currentTime = new Date()
        var hours = currentTime.getHours()
        var minutes = currentTime.getMinutes()
        var seconds = currentTime.getSeconds();
        var HistoryTime = hours + ':' + minutes + ':' + seconds;
        
        $.ajax({
            url: "/WebServices/History.ashx?method=saveNewHistory",
            data: {
                HistoryType: 'Application', contactid: '0', hdate: HistoryDate, htime: HistoryTime, action: hAction,
                user: userName, description: hDesc, appid: getQuerystring('app_id', 0)
            },
            success: function (result) {                
                window.location.reload();
                $('#dlgAppCombine').dialog('close');
                //$('#dgApplicationHistory').datagrid('reload');
            }
        });

    }
    function InsertCombineApp() {

        var app_id = getQuerystring('app_id', 0);
        var contactID = '';
        getEachRadioButtonValue();

        if ($("#hdnFirstContactId").val() != "" && $("#hdnFirstSelect").val() != "") {
            contactID = $("#hdnFirstContactId").val() + "_" + $("#hdnFirstSelect").val() + "_" + $("#CombineApp1").val() + " , "

        }
        if ($("#hdnSecondContactId").val() != "" && $("#hdnSecondSelect").val() != "") {
            contactID = contactID + $("#hdnSecondContactId").val() + "_" + $("#hdnSecondSelect").val() + "_" + $("#CombineApp2").val() + ",";
        }
        if ($("#hdnThirdContactId").val() != "" && $("#hdnThirdSelect").val() != "") {
            contactID = contactID + $("#hdnThirdContactId").val() + "_" + $("#hdnThirdSelect").val() + "_" + $("#CombineApp3").val();
        }

        var c_Id = contactID.substr(0, contactID.length - 1);
        var arryContactidList = c_Id.split(',');
        var lengthConteactidList = arryContactidList.length;
        var arryLastContact = arryContactidList[lengthConteactidList - 1];
        var c_app_id = $("#txtappid").val();

        var combineAppId;

        //var combineAppVal = contactID.split('_')[2];
        var combineAppVal = '';
        if (arryLastContact.length > 0)
            combineAppVal = arryLastContact.split('_')[2];
        else
            combineAppVal = contactID.split('_')[2];

        var combineApp = combineAppVal.replace(',', '').replace(/^\s+|\s+$/g, '');

        if (combineApp == "null")
            combineAppId = "";
        else
            combineAppId = combineApp;

        //History info
        var conID = '';
        if ($("#hdnFirstContactId").val() != "" && $("#hdnFirstSelect").val() != "")
            conID = $("#hdnFirstContactId").val() + ",";

        if ($("#hdnSecondContactId").val() != "" && $("#hdnSecondSelect").val() != "")
            conID = conID + $("#hdnSecondContactId").val() + ",";

        if ($("#dnThirdContactId").val() != "" && $("#hdnThirdSelect").val() != "")
            conID = conID + $("#hdnThirdContactId").val();

        $.ajax({
            type: "Post",
            data: { app_id: app_id, contactID: contactID, c_app_id: c_app_id, combineAppId: combineAppId },
            url: "/WebServices/InsertCombineApplicantData.ashx",
            success: function (data) {

                combineAppVal = '';
                arryLastContact = '';
                contactID = '';
                combineAppId = '';
                combineApp = '';

                //switch case according to result:
                switch (data) {
                    
                    case "1":
                        var Description = "<span style='color:black;font-weight: bold'>" + "ApplicationID: " + app_id + "</span>" + " Combine with ApplicationID: " + "<span style='color:green;font-weight: bold'>" + c_app_id + "</span>" + " and ClientID: " + "<span style='color:green;font-weight: bold'>" + conID + "</span>" + "<br/>";
                        var Action = 'Combine';
                        saveHistory(Action, Description);
                        break;
                    case "11":
                        var Description = "<span style='color:black;font-weight: bold'>" + "ApplicationID: " + app_id + "</span>" + " Combine with ApplicationID: " + "<span style='color:green;font-weight: bold'>" + c_app_id + "</span>" + " and ClientID: " + "<span style='color:green;font-weight: bold'>" + conID + "</span>" + "<br/>";
                        var Action = 'Combine';
                        saveHistory(Action, Description);
                        break;

                    case "111":
                        var Description = "<span style='color:black;font-weight: bold'>" + "ApplicationID: " + app_id + "</span>" + " Combine with ApplicationID: " + "<span style='color:green;font-weight: bold'>" + c_app_id + "</span>" + " and ClientID: " + "<span style='color:green;font-weight: bold'>" + conID + "</span>" + "<br/>";
                        var Action = 'Combine';
                        saveHistory(Action, Description);
                        break;
                    case "2":  /*Second... */

                        var Description = "<span style='color:black;font-weight: bold'>" + "ApplicationID: " + app_id + "</span>" + " Delete combined application : " + "<span style='color:green;font-weight: bold'>" + c_app_id + "</span>" + " and ClientID: " + "<span style='color:green;font-weight: bold'>" + conID + "</span>" + "<br/>";
                        var Action = 'Remove CombineApp';
                        saveHistory(Action, Description);
                        break;

                    case "22":  /*Second... */
                        var Description = "<span style='color:black;font-weight: bold'>" + "ApplicationID: " + app_id + "</span>" + " Delete combined application : " + "<span style='color:green;font-weight: bold'>" + c_app_id + "</span>" + " and ClientID: " + "<span style='color:green;font-weight: bold'>" + conID + "</span>" + "<br/>";
                        var Action = 'Remove CombineApp';
                        saveHistory(Action, Description);
                        break;
                    case "222":  /*Second... */
                        var Description = "<span style='color:black;font-weight: bold'>" + "ApplicationID: " +app_id+ "</span>" + " Delete combined application : " + "<span style='color:green;font-weight: bold'>" + c_app_id + "</span>" + " and ClientID: " + "<span style='color:green;font-weight: bold'>" + conID + "</span>" + "<br/>";
                        var Action = 'Remove CombineApp';
                        saveHistory(Action, Description);
                        break;
                }

                //combineAppId = '';
                //combineApp = '';
                //$("#txtappid").val('');
                //var tab = $('#MainTab').tabs('getTab', 'Verification Checklist'); // get selected panel
                //tab.panel('refresh', '/Pages/Application/VerificationChecklist.aspx?app_id=' +app_id);
                //// alert("Application Combined");
             
                
            },
            error: function (result) {
                $.messager.alert('Morgan CRM',"Application Combined Falied");
            }
        });

    }
</script>
